<?php

namespace Management\Controller;

use Think\Controller;

use Myself\FileSystem;

class DriverController extends BaseController {



	protected $m = NULL;

	protected $m_class = NULL;

	protected $m_config = NULL;

    protected $statusArr=array("未认证","已认证");

	

	public function _initialize(){

		parent::_initialize();

		$this->m = M('driver');

		

	}

		

	

	

    public function index(){

		header('Content-Type:text/html;charset=utf-8');

		$where=[];

		//-----------------搜索-------------------
		if(I('dtype')){
			$where['type'] = I('dtype');
			$this->assign('type',I('dtype'));
		}
		if(I('realname')){
			$where['realname'] = array('like','%'.I('realname').'%');
			// $this->assign('type',I('dtype'));
		}


		//-------------------------搜索end-----------------

		$pageno = I('p',1,'int');

		$pageCount = 10;

		$list = $this->m->where($where)->page($pageno.','.$pageCount)->select();
		if(!empty($list)){
			foreach ($list as $k => $v) {
				$list[$k]['car_no'] = M('car')->where(['car_id'=>$v['car_id']])->getField('car_no');
			}
		}
		$this->assign('list',$list);// 赋值数据集

		$count = $this->m->where($where)->count();// 查询满足要求的总记录数

		$this->assign('count',$count);// 赋值分页输出	

		$Page = new \Think\Page($count,$pageCount);// 实例化分页类 传入总记录数和每页显示的记录数

		$show = $Page->showManage();// 分页显示输出

		$this->assign('page',$show);// 赋值分页输出

        //-------------------------固定参数输出-----------------

        $this->assign('statusArr',$this->statusArr);// 状态
        

		$this->display('index');

    }

	public function edit(){

		$id = I('id',0,'int');

		$dataInfo = $this->m->find($id);
		$dataInfo['cartype'] =M('cartype')->where(['id'=>$dataInfo['cartype_id']])->getField('title');
		$this->assign('dataInfo',$dataInfo);

		$this->assign('back_url',$_SERVER['HTTP_REFERER']);
		$this->assign('car',M('car')->select());// 车辆


		$this->display('edit');

    }

	public function save(){
		$temp = $this->m->create();
        $pic_new=I('post.pic_path_new');
        if($pic_new){
            $temp['pic_path']=$this->pic_move($pic_new,$temp['pic_path']);
        }
        $type= $this->m->where(['id'=>$temp['id']])->getField('type');//1是司机2是工人
		if(!empty($temp['car_id']) && $type==1){
			//绑定了车辆 身份是司机 则清空其他司机绑定该车
			$rrr = $this->m->where(['car_id'=>$temp['car_id'],'type'=>1])->save(['car_id'=>'']);
		}
        // echo $type;
        // ll($temp);die;
		$res = $this->m->save($temp);
		if($res){
			//修改司机成功
			$this->selectPage_ajax(array('act' => '绑定', 'back_url' => I('back_url'),'jump_url' => U('Driver/index','id='.$temp['id'])));exit;
		}else{
			$this->error($GLOBALS['notice']['error'][0]);exit;
		}

	}
	public function driver_save(){
		$temp = $this->m->create();
		$isset = $this->m->where(['username'=>$temp['username']])->find();
		if($isset){
			$this->selectPage_ajax(['act'=>'账号已存在重新输入，操作未']);
		}
		// $temp['pwd'] = md5($temp['pwd']);
		$result = $this->m->add($temp);
		if($result){
		    $this->selectPage_ajax( array('act' => '添加', 'back_url' =>U('Driver/index','id='.$temp['id']),'jump_url' => U('Driver/index','id='.$temp['id'])));
		}else{
			$this->selectPage_ajax(['act'=>'操作未']);
		}
	}
	public function driver_edit(){
		$this->display('driver_edit');
	}

	public function del(){



		

		$ids = I('id');



		if($ids){

			if(is_array($ids)){$ids=implode(",",$ids);}

			$this->baseDelFile('cartype',$ids,'pic_path');

			if ($this->m->delete($ids)){

				$ids=explode(",",$ids);

			    $this->success($GLOBALS['notice']['success'][1]);

			}else{

				$this->error($GLOBALS['notice']['error'][1]);

			}

		}else{

			$this->error($GLOBALS['notice']['warming'][1]);

		}		

    }

	public function batch(){          ////批量操作方法



		

		$ids = I('id');

		$val = I('get.val');

		$field = I('get.field');

		

		

		

		if($ids){

			if(is_array($ids)){$ids=implode(",",$ids);}

			

			if ($this->m->where('id in ('.$ids.')')->save(array($field=>$val))){

				$ids=explode(",",$ids);

			    $this->success($GLOBALS['notice']['success'][2]);

			}else{

				$this->error($GLOBALS['notice']['error'][2]);

			}

		}else{

			$this->error($GLOBALS['notice']['warming'][1]);

		}		

    }

	public function ajaxFindNext(){



		$selValue = I('selValue','0','int');

		$areaNextId = I('areaNextId','','int');

		$domId = I('domId');

		$classList = $selValue?$this->m_class->where('parent_id='.$selValue)->select():array();

		$content = "";

		if(!count($classList)){

			echo 0;exit;

		}else{

		

			$content .= '<span id="'.$domId.'_'.$areaNextId.'">'."\n\t";

			$content .= '<select class="form-control input-small" name="class_id[]" onchange="javascript:findNext(this.value , '.$areaNextId.' , \''.$domId.'\')">'."\n\t";

			$content .= '<option value="0">请选择</option>'."\n\t";

			foreach($classList as $item){

			$selectedMark = $selValue==$item['id']?'selected':'';

			$content .= '<option value="'.$item['id'].'" '.$selectedMark.'>'.$item['class_name'].'</option>'."\n\t";

			}

			$content .= '</select>'."\n\t";

			$content .= '</span>'."\n\t";

		

			echo $content;exit;

		

		}

	}

	public function ajaxListInput(){

		header('Content-Type:text/html;charset=utf-8');

		$info = I('post.');

		$data[$info["fieldName"]] = $info["Val"];

		$data["id"] = $info["id"];

		if ($this->m->save($data)){

			echo "success";

		}else{

			echo "error";

		}

		exit;

	}

	

	

	

	

}